//
//  SXPUserModel.swift
//  weibo
//
//  Created by shixinPeng on 16/2/27.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit
/*
Optional({
"access_token" = "2.00dz11gC0hDkYL6b5e70b729vssGOE";
"expires_in" = 157679999;
"remind_in" = 157679999;
uid = 2457719037;
*/
class SXPUserModel: NSObject, NSCoding {
 /// 获取的token,用来请求用户数据
    var access_token:String?
 /// 允许登陆的时间,单位为秒
    var expires_in: NSTimeInterval = 0 {
        didSet {
            
            //记录token的时效
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
            //print(expires_date)
   
        }
    }
    var expires_date: NSDate?
 ///放弃的接口
    //var remind_in:
 /// 当前用户授权的uid
    var uid: String?
    //MARK: 保存获取用户信息的属性
    /// 个人姓名
    var screen_name: String?
    var profile_image_url: String?
    
    //KVC构造方法
     init(dict: [String: NSObject]) {
        super.init()
        //kvc
        setValuesForKeysWithDictionary(dict)
        
    }
    //防止key出错
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    /// 解归档路径
    private static let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathExtension("userModel.archive")!
    
    
    //获取沙盒路径,把模型归档
    func save(cacheStr:String){
        
        //写入
        NSKeyedArchiver.archiveRootObject(self, toFile: SXPUserModel.path)
        //print(self)
        
    }
    
    //解档,获取模型数据
    class func readUserDate() ->SXPUserModel? {
        
        //读取数据
        if let model = NSKeyedUnarchiver.unarchiveObjectWithFile(SXPUserModel.path) as? SXPUserModel {
        
            if model.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            
            return model
            
            }
        }
        return nil
    }
    
    //MARK:解档归档方法
    ///  归档
     func encodeWithCoder(aCoder: NSCoder){
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
    }
    ///  解档
     required init?(coder aDecoder: NSCoder){
        
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        self.expires_in = aDecoder.decodeObjectForKey("expires_in") as! NSTimeInterval
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
        self.expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        
        
    }
    
    override var description: String {
        
        let keys = ["access_token","expires_in","uid","screen_name","profile_image_url","expires_date"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    
    
    
}
